package com.codedancer.campus.manager.schedule;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.codedancer.campus.common.dataresource.HanData;
import com.codedancer.campus.common.model.han.ZhiHuHotModel;
import com.codedancer.campus.common.utils.UUIDGenerator;
import com.codedancer.campus.mapper.HeadlineNewsMapper;
import com.codedancer.campus.po.HeadlineNews;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 知乎数据拉取定时任务
 */
@Component
@Slf4j
public class ZhiHuTopDataSchedule {

    @Autowired
    private HanData hanData;
    @Autowired
    private HeadlineNewsMapper headlineNewsMapper;

    /**
     * 每隔一分钟拉取知乎热榜数据
     */
//    @Scheduled(cron = "0 0/1 * * * ?")
//    public void pullZhiHuHotData() {
//        List<ZhiHuHotModel> zhiHuHotModels =
//            hanData.getZhiHuHotData();
//        int putStorage = 0;
//        if (CollUtil.isNotEmpty(zhiHuHotModels)) {
//            for (ZhiHuHotModel zhiHuHotModel : zhiHuHotModels) {
//                // 根据标题判断是否存在
//                if (headlineNewsMapper.existsByColumn("title", zhiHuHotModel.getTitle())) {
//                    continue;
//                }
//                HeadlineNews headlineNews = BeanUtil.copyProperties(zhiHuHotModel,
//                    HeadlineNews.class);
//                headlineNews.setUuid(UUIDGenerator.getUUID());
//                headlineNews.setDescription(zhiHuHotModel.getDesc());
//                headlineNews.setCoverImage(zhiHuHotModel.getPic());
//                headlineNews.setUrl(zhiHuHotModel.getMobilUrl());
//                headlineNews.setSource("知乎");
//                headlineNews.setPublishDate(new Date());
//                headlineNewsMapper.insert(headlineNews);
//                putStorage++;
//            }
//            log.info("知乎热榜拉取成功，入库成功，入库数量：{}", putStorage);
//            return;
//        }
//        log.info("拉取的知乎热榜数据为空");
//    }
}
